package view;

import java.util.Observable;
import java.util.Observer;

import model.Point;

/**
 * Created with IntelliJ IDEA.
 * User: phuc_lagger
 * Date: 11/10/13
 * Time: 4:01 PM
 * To change this template use File | Settings | File Templates.
 */
public class TextScreen {
    private final int nRows; // number of rows
    private final int nCols; // number of columns
    private char[][] screen; // first dimension: nRows; second dimension: nCols

    public TextScreen(int nRows, int nCols) {
        this.nRows = nRows;
        this.nCols = nCols;
        screen = new char[nRows][nCols];

        // draw top and bottom borders
        for (int i = 1; i < nCols - 1; i++) {
            screen[0][i] = '-';
            screen[nRows - 1][i] = '-';
        }

        // draw corners
        screen[0][0] = '+';
        screen[0][nCols - 1] = '+';
        screen[nRows - 1][0] = '+';
        screen[nRows - 1][nCols - 1] = '+';

        // draw left and right borders
        for (int i = 1; i < nRows - 1; i++) {
            screen[i][0] = '|';
            screen[i][nCols - 1] = '|';
            for (int j = 1; j < nCols - 1; j++) {
                screen[i][j] = ' ';
            }
        }
    }

    public String getRow(int row) {
        return new String(screen[row]);
    }

    public void setChar(int x, int y, char c) {
        try {
            screen[y][x] = c;
        } catch (ArrayIndexOutOfBoundsException e) {
        }

    }

    public void drawAlien(Point topLeft) {
        this.setChar(topLeft.x, topLeft.y, '\\');
        this.setChar(topLeft.x + 1, topLeft.y, '/');
        this.setChar(topLeft.x + 2, topLeft.y, '|');
        this.setChar(topLeft.x + 3, topLeft.y, '\\');
        this.setChar(topLeft.x + 4, topLeft.y, '/');

        this.setChar(topLeft.x, topLeft.y + 1, '/');
        this.setChar(topLeft.x + 1, topLeft.y + 1, '\\');
        this.setChar(topLeft.x + 2, topLeft.y + 1, 'O');
        this.setChar(topLeft.x + 3, topLeft.y + 1, '/');
        this.setChar(topLeft.x + 4, topLeft.y + 1, '\\');
    }

    public void removeAlien(Point topLeft) {
        this.setChar(topLeft.x, topLeft.y, ' ');
        this.setChar(topLeft.x + 1, topLeft.y, ' ');
        this.setChar(topLeft.x + 2, topLeft.y, ' ');
        this.setChar(topLeft.x + 3, topLeft.y, ' ');
        this.setChar(topLeft.x + 4, topLeft.y, ' ');

        this.setChar(topLeft.x, topLeft.y + 1, ' ');
        this.setChar(topLeft.x + 1, topLeft.y + 1, ' ');
        this.setChar(topLeft.x + 2, topLeft.y + 1, ' ');
        this.setChar(topLeft.x + 3, topLeft.y + 1, ' ');
        this.setChar(topLeft.x + 4, topLeft.y + 1, ' ');
    }

    public void drawTank(Point topLeft) {
        this.setChar(topLeft.x + 3, topLeft.y, '|');

        this.setChar(topLeft.x, topLeft.y + 1, '>');
        this.setChar(topLeft.x + 1, topLeft.y + 1, '|');
        this.setChar(topLeft.x + 2, topLeft.y + 1, '-');
        this.setChar(topLeft.x + 3, topLeft.y + 1, '-');
        this.setChar(topLeft.x + 4, topLeft.y + 1, '-');
        this.setChar(topLeft.x + 5, topLeft.y + 1, '|');
        this.setChar(topLeft.x + 6, topLeft.y + 1, '<');

        this.setChar(topLeft.x, topLeft.y + 2, '|');
        this.setChar(topLeft.x + 1, topLeft.y + 2, '|');
        this.setChar(topLeft.x + 2, topLeft.y + 2, '-');
        this.setChar(topLeft.x + 3, topLeft.y + 2, '-');
        this.setChar(topLeft.x + 4, topLeft.y + 2, '-');
        this.setChar(topLeft.x + 5, topLeft.y + 2, '|');
        this.setChar(topLeft.x + 6, topLeft.y + 2, '|');
    }

    public void removeTank(Point topLeft) {
        this.setChar(topLeft.x, topLeft.y, ' ');
        this.setChar(topLeft.x + 1, topLeft.y, ' ');
        this.setChar(topLeft.x + 2, topLeft.y, ' ');
        this.setChar(topLeft.x + 3, topLeft.y, ' ');
        this.setChar(topLeft.x + 4, topLeft.y, ' ');
        this.setChar(topLeft.x + 5, topLeft.y, ' ');
        this.setChar(topLeft.x + 6, topLeft.y, ' ');

        this.setChar(topLeft.x, topLeft.y + 1, ' ');
        this.setChar(topLeft.x + 1, topLeft.y + 1, ' ');
        this.setChar(topLeft.x + 2, topLeft.y + 1, ' ');
        this.setChar(topLeft.x + 3, topLeft.y + 1, ' ');
        this.setChar(topLeft.x + 4, topLeft.y + 1, ' ');
        this.setChar(topLeft.x + 5, topLeft.y + 1, ' ');
        this.setChar(topLeft.x + 6, topLeft.y + 1, ' ');

        this.setChar(topLeft.x, topLeft.y + 2, ' ');
        this.setChar(topLeft.x + 1, topLeft.y + 2, ' ');
        this.setChar(topLeft.x + 2, topLeft.y + 2, ' ');
        this.setChar(topLeft.x + 3, topLeft.y + 2, ' ');
        this.setChar(topLeft.x + 4, topLeft.y + 2, ' ');
        this.setChar(topLeft.x + 5, topLeft.y + 2, ' ');
        this.setChar(topLeft.x + 6, topLeft.y + 2, ' ');
    }

    public void drawBullet(Point point){
        this.setChar(point.x,point.y, '^');
    }

    public void removeBullet(Point point) {
        this.setChar(point.x, point.y, ' ');
    }


}
